﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Models;
using RMS.Models.Repository;
using RMS.Pages.Helpers;
using System.Web.Routing;
using RMS.Pages;

namespace RMS.Pages
{
    public partial class CartView : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {
            // Refresh browser cache
            Response.AddHeader("Expires", "0");
            
            if (IsPostBack) {

                Repository repo = new Repository();
                int Ret_ID;

                if (int.TryParse(Request.Form["remove"], out Ret_ID)) {
                    Retreat retreatToRemove = repo.Retreats
                    .Where(p => p.Ret_ID == Ret_ID).FirstOrDefault();
                    if (retreatToRemove != null) {
                        SessionHelper.GetCart(Session).RemoveLine(retreatToRemove);
                    }
                    // Refreshes page on item removal to avoid PostBack issues
                    Response.Redirect(Request.RawUrl);
                }

                DisplayCart();
                canRegisterButtonVisible();
                continueButtonSection.Visible = true;
            }

            DisplayCart();
            canRegisterButtonVisible();
            continueButtonSection.Visible = true;
        }

        protected void DisplayCart()
        {
            if (CountCartLines() == 0)
            {
                CartSection.Visible = false;
                EmptyCart.Visible = true;
            }
            else
            {
                CartSection.Visible = true;
                EmptyCart.Visible = false;
            }
        }

        protected void canRegisterButtonVisible()
        {
            if ((CountCartLines() == 0) || (CountCartLines() > 1))
            {
                registerButtonSection.Visible = false;
            }
            else
            {
                registerButtonSection.Visible = true;
            }
        }
           

        public IEnumerable<CartLine> GetCartLines()
        {
            return SessionHelper.GetCart(Session).Lines;
        }

        public int CountCartLines()
        {
            int count = 0;
            
            foreach (CartLine i in GetCartLines()) {
                count++;
            }

            return count;
        }

        public decimal CartTotal {
            get
            {
                return SessionHelper.GetCart(Session).ComputeTotalValue();
            }
        }
                
        public string ReturnUrl {
            get
            {
                String defaultPage = "/Pages/Listing.aspx";
                String toReturn = SessionHelper.Get<string>(Session, SessionKey.RETURN_URL);

                if (toReturn == null)
                {
                    return defaultPage;
                }
                else
                {
                    return toReturn;
                }

                //return SessionHelper.Get<string>(Session, SessionKey.RETURN_URL);
            }
        }

        public string CheckoutUrl
        {
            get
            {
                return RouteTable.Routes.GetVirtualPath(null, "checkout",
                null).VirtualPath;
            }
        }

        protected void btnRegisterForRetreat_Click(object sender, EventArgs e)
        {
            Response.Redirect(CheckoutUrl);
        }

        protected void btnContinueBrowsing_Click(object sender, EventArgs e)
        {
            Response.Redirect(ReturnUrl);
        }
    }
}